# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2025/3/15
desc: 
sample: 
"""
import os

from locust import HttpUser, task, SequentialTaskSet, events
from gevent._semaphore import Semaphore
all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire() #信号量占用

@events.spawning_complete.add_listener
def on_hatch_complete(**kwargs):
    all_locusts_spawned.release()

class UserBehavior(SequentialTaskSet):
    @task(3)
    def index(self):
        print("index")

    @task(1)
    def profile(self):
        print("profile")


class WebsiteUser(HttpUser):
    host="http://www.baidu.com"
    tasks  = [UserBehavior]
    min_wait = 2000
    max_wait = 5000
    def on_start(self):
        all_locusts_spawned.wait()  # 核心等待信号量
        print("on_start")



if __name__ == '__main__':
    os.system('locust -f UserBehavior.py')